﻿// Example header text. Can be configured in the options.
using System;
using System.Data;
using System.Windows.Forms;
using vtech_frameWorks.Common;
using vtech_frameWorks.Exceptions;
using vtech_frameWorks.Library;
using vtech_system.User;

namespace vtech_system
{
    public partial class frmLogin : vtech_formBase.Object.frmObject
    {
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:
                    Application.Exit();
                    return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private static frmLogin m_oInstance = null;

        public static frmLogin GetInstance()
        {
            if (m_oInstance == null)
            {
                m_oInstance = new frmLogin();
            }
            return m_oInstance;
        }

        public frmLogin()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Check Control
        /// </summary>
        /// <returns></returns>
        private bool CheckControl()
        {
            try
            {
                if (this.txtUsername.Text == "")
                {
                    this.errorProvider.SetError(this.txtUsername, "Tên đăng nhập không được để trống");
                    return false;
                }
                else
                {
                    if (this.txtPassword.Text == "")
                    {
                        this.errorProvider.SetError(this.txtPassword, "Mật khẩu không được để trống");
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            catch
            {
                return false;
            }
        }

        private bool isLogin(string userName, string passWord)
        {
            bool flag = false;
            try
            {
                ClsUserBS obj = new ClsUserBS();
                this.dsUser.DtUser.Clear();
                this.dsUser.DtUser.Merge(obj.GetAll());

                DataRow[] dRow = this.dsUser.Tables["DtUser"].Select(string.Format("UserName='{0}' AND Password='{1}'", userName, passWord));
                foreach (DataRow row in dRow)
                {
                    ClsGlobal.UserID = (Guid)row["ID"];
                    flag = true;
                }
            }
            catch
            {
                flag = false;
            }
            return flag;
        }

        private void loginSystem()
        {
            try
            {
                if (this.CheckControl())
                {
                    if (this.isLogin(this.txtUsername.Text.Trim(), vtech_frameWorks.Library.Encryption.ClsMd5.Encrypt(this.txtPassword.Text.Trim(), ClsGlobal.strPrivateKey, true)))
                    {
                        this.Close();
                    }
                    else
                    {
                        ClsInformation.Error("Sai thông tin đăng nhập");
                    }
                }
                else
                {
                    ClsInformation.Error("Thiếu thông tin đăng nhập !");
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }
        
        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                Application.Exit();
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                this.loginSystem();
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            txtUsername.Text = ClsUtilities.GetConfigKey("Username");
            txtPassword.Text = ClsUtilities.GetConfigKey("Password");
        }

        private void chkRemember_ToggleStateChanged(object sender, Telerik.WinControls.UI.StateChangedEventArgs args)
        {
            if (chkRemember.Checked == true)
            {
                ClsUtilities.SetConfigKey("Username", txtUsername.Text.Trim());
                ClsUtilities.SetConfigKey("Password", txtPassword.Text.Trim());
            }
        }
    }
}